package com.xhx.accounting.ui.me;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xhx.accounting.DBDao;
import com.xhx.accounting.R;
import com.xhx.accounting.currency_exchange;
import com.xhx.accounting.databinding.FragmentMeBinding;
import com.xhx.accounting.loan;
import com.xhx.accounting.fund;
import com.xhx.accounting.stock;
import com.xhx.accounting.bank;
import com.xhx.accounting.bond;
import com.xhx.accounting.setting;

import java.text.DecimalFormat;
import java.util.Calendar;

public class me extends Fragment {
    private FragmentMeBinding binding;
    private View currency,setting,loan,fund,stock,bank,bond;
    private TextView budgetMoney,cost,remain;
    private DBDao db;
    private Long startTime, endTime;
    private DecimalFormat decimalFormat;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        binding = FragmentMeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        db = new DBDao(getActivity());

        //initData
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0); //获取当前月第一天
        c.set(Calendar.DAY_OF_MONTH, 1); //设置为1号,当前日期既为本月第一天
        c.set(Calendar.HOUR_OF_DAY, 0); //将小时至0
        c.set(Calendar.MINUTE, 0); //将分钟至0
        c.set(Calendar.SECOND,0); //将秒至0
        c.set(Calendar.MILLISECOND, 0); //将毫秒至0
        startTime = c.getTimeInMillis() / 1000;
        Calendar c2 = Calendar.getInstance();
        c2.set(Calendar.DAY_OF_MONTH, c2.getActualMaximum(Calendar.DAY_OF_MONTH)); //获取当前月最后一天
        c2.set(Calendar.HOUR_OF_DAY, 23); //将小时至23
        c2.set(Calendar.MINUTE, 59); //将分钟至59
        c2.set(Calendar.SECOND,59); //将秒至59
        c2.set(Calendar.MILLISECOND, 999); //将毫秒至999
        endTime = c2.getTimeInMillis()/1000;

        currency = root.findViewById(R.id.currency_exchange);
        currency.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), currency_exchange.class);
                startActivity(intent);
            }

        });

        setting = root.findViewById(R.id.setting_box);
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), setting.class);
                startActivity(intent);

            }
        });

        loan = root.findViewById(R.id.loan_box);
        loan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), loan.class);
                startActivity(intent);

            }
        });

        fund = root.findViewById(R.id.fund_box);
        fund.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), fund.class);
                startActivity(intent);
            }
        });

        stock = root.findViewById(R.id.stock_box);
        stock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), stock.class);
                startActivity(intent);
            }
        });

        bank = root.findViewById(R.id.bank_box);
        bank.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), bank.class);
                startActivity(intent);
            }
        });

        bond = root.findViewById(R.id.bond_box);
        bond.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), bank.class);
                startActivity(intent);
            }
        });

        budgetMoney = root.findViewById(R.id.budget_money);
        cost = root.findViewById(R.id.cost);
        remain = root.findViewById(R.id.remain_money);

        decimalFormat = new DecimalFormat("0.00");

        return root;
    }
    @Override
    public void onStart() {
        super.onStart();
        int x = db.getBudget();
        double y = db.getRecord3(startTime,endTime);
        double z = Double.valueOf(x) - y;
        budgetMoney.setText(String.valueOf(x));
        cost.setText(String.valueOf(y));
        remain.setText(decimalFormat.format(z));
    }
}